package com.chinabank.web.controller;

import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.chinabank.model.Account;
import com.chinabank.model.Group;
import com.chinabank.model.Permission;
import com.chinabank.service.AccountService;
import com.chinabank.service.GroupService;
import com.chinabank.service.PermissionService;
import com.chinabank.util.MD5;


@Controller
@RequestMapping("/admin")
public class AdminController {
	private AccountService accountService;
	private GroupService groupService;
	private PermissionService permissionService;
	
	public PermissionService getPermissionService() {
		return permissionService;
	}

	@Resource
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public AccountService getAccountService() {
		return accountService;
	}

	@Resource
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	@Resource
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	@RequestMapping(value = "/home.do", method = RequestMethod.GET)
	public String adminHome(HttpServletRequest request, ModelMap model)
			throws ParseException {
		return "forward:/WEB-INF/admin/index.jsp";
	}

	@RequestMapping(value = "/user/add.do", method = RequestMethod.GET)
	public String displayAddAccountForm(HttpServletRequest request,
			ModelMap model) throws ParseException {
		List<Group> groups = groupService.getGroups();
		model.put("groups", groups);
		return "forward:/WEB-INF/admin/signin.jsp";
	}

	@RequestMapping(value = "/user/add.do", method = RequestMethod.POST)
	public String addAccount(HttpServletRequest request, ModelMap model) {
		String username = request.getParameter("username").trim();
		String password = request.getParameter("password").trim();
		String description = request.getParameter("description").trim();
		String email = request.getParameter("email").trim();
		Long groupId = Long.parseLong(request.getParameter("groupId").trim());

		Group grp = groupService.getGroup(groupId);
		password = MD5.getMD5Code(password);
		Account account = new Account(username, password, description, email,
				grp);

		if (grp != null && !username.equals("") && !password.equals("")) {
			accountService.addAccount(account);
			model.put("info", "用户添加成功");
			return "redirect:/admin/home.do";
		}
		model.put("error", "输入有误");
		return "forward:/WEB-INF/admin/signin.jsp";
	}

	@RequestMapping(value = "/user/list.do", method = RequestMethod.GET)
	public String listAccount(HttpServletRequest request, ModelMap model)
			throws ParseException {
		List<Account> accounts = accountService.listAccount();
		model.put("accounts", accounts);
		return "forward:/WEB-INF/admin/users.jsp";
	}

	@RequestMapping(value = "/user/del.do", method = RequestMethod.GET)
	public String delAccount(HttpServletRequest request, ModelMap model)
			throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		Account account;
		try {
			
			accountService.delAccount(id);
			return "redirect:/admin/user/list.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}

	@RequestMapping(value = "/user/edit.do", method = RequestMethod.GET)
	public String displayEditAccountFrom(HttpServletRequest request,
			ModelMap model) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		Account account;
		try {
			account = accountService.getAccount(id);
			List<Group> groups = groupService.getGroups();
			model.put("groups", groups);
			model.put("account", account);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/WEB-INF/admin/user.jsp";
	}

	/**
	 * @param request
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/user/edit.do", method = RequestMethod.POST)
	public String editAccount(HttpServletRequest request, ModelMap model)
			throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		String username = request.getParameter("username").trim();
		String description = request.getParameter("description").trim();
		String email = request.getParameter("email").trim();
		Long groupId = Long.parseLong(request.getParameter("groupId").trim());

		Account account;
		try {
			account = accountService.getAccount(id);
			Group gp = groupService.getGroup(groupId);
			account.setUsername(username);
			account.setEmail(email);
			account.setDescription(description);
			account.setMyGroup(gp);
			accountService.updateAccount(account);
			return "redirect:/admin/user/list.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}


	
	@RequestMapping(value = "/group/list.do", method = RequestMethod.GET)
	public String listGroup(HttpServletRequest request, ModelMap model)
			throws ParseException {
		List<Group> gps;
		try {
			gps = groupService.getGroups();
			model.put("groups", gps);
			return "forward:/WEB-INF/admin/groups.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}

	@RequestMapping(value = "/group/add.do", method = RequestMethod.GET)
	public String displayAddGroupForm(HttpServletRequest request, ModelMap model)
			throws ParseException {
		return "forward:/WEB-INF/admin/addGroup.jsp";
	}

	@RequestMapping(value = "/group/add.do", method = RequestMethod.POST)
	public String addGroup(HttpServletRequest request, ModelMap model)
			throws ParseException {
		String name = request.getParameter("name").trim();
		String description = request.getParameter("description").trim();
		Group gp = new Group();
		gp.setName(name);
		gp.setDescription(description);
		try{
			groupService.addGroup(gp);
			return "redirect:/admin/group/list.do";
		} catch(Exception e){
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}
	
	@RequestMapping(value = "/group/edit.do", method = RequestMethod.GET)
	public String displayEditGroupForm(HttpServletRequest request,
			ModelMap model) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		Group gp;
		try {
			gp = groupService.getGroup(id);
			model.put("group", gp);
			return "forward:/WEB-INF/admin/group.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}

	@RequestMapping(value = "/group/edit.do", method = RequestMethod.POST)
	public String editGroup(HttpServletRequest request,
			ModelMap model) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		String name = request.getParameter("name").trim();
		String description = request.getParameter("description").trim();
		Group gp;
		try {
			gp = groupService.getGroup(id);
			gp.setName(name);
			gp.setDescription(description);
			groupService.updateGroup(gp);
			return "redirect:/admin/group/list.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}
	
	@RequestMapping(value = "/group/del.do", method = RequestMethod.GET)
	public String delGroup(HttpServletRequest request,
			ModelMap model) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		Group gp;
		try {
			gp = groupService.getGroup(id);
			groupService.delGroup(gp);
			return "redirect:/admin/group/list.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}
	
	
	@RequestMapping(value = "/permission/list.do", method = RequestMethod.GET)
	public String listPermission(HttpServletRequest request, ModelMap model)
			throws ParseException {
		List<Permission> pms;
		try {
			pms = permissionService.getPermissions();
			model.put("permissions", pms);
			return "forward:/WEB-INF/admin/permissions.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}

	@RequestMapping(value = "/permission/add.do", method = RequestMethod.GET)
	public String displayAddPermissionForm(HttpServletRequest request, ModelMap model)
			throws ParseException {
		return "forward:/WEB-INF/admin/addPermission.jsp";
	}

	@RequestMapping(value = "/permission/add.do", method = RequestMethod.POST)
	public String addPermission(HttpServletRequest request, ModelMap model)
			throws ParseException {
		String url = request.getParameter("url").trim();
		
		Permission p = new Permission();
		p.setUrl(url);
		try{
			permissionService.addPermission(p);
			return "redirect:/admin/permission/list.do";
		} catch(Exception e){
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}
	
	@RequestMapping(value = "/permission/del.do", method = RequestMethod.GET)
	public String delPermission(HttpServletRequest request,
			ModelMap model) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		Permission p;
		try {
			p = permissionService.getPermission(id);
			permissionService.delPermission(p);
			return "redirect:/admin/permission/list.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}

	@RequestMapping(value = "/permission/manage.do", method = RequestMethod.GET)
	public String managePermission(HttpServletRequest request,
			ModelMap model) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		Permission p;
		try {
			p = permissionService.getPermission(id);
			permissionService.delPermission(p);
			return "redirect:/admin/permission/list.do";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/public/500.jsp";
	}
	
}
